<?php
 /**
   * This file contains the function to return a fieldspec string for the fieldspec array describint the crudset  
   * @package phpnewmaker
   */
   
include_once "../../pdo_boot.php";





function makesuredir($dir){
	if(!file_exists(dirname($dir))){
		makesuredir(dirname($dir));
	}
	if(!file_exists($dir)){
		mkdir($dir);
	}
}

function write_fieldspecs($crudset_id){

$ar_doquote['show'] = '';
$ar_doquote['edit'] = '';
$ar_doquote['key'] = '';
$ar_doquote['filter'] = '';
$ar_doquote['sort'] = '';
$ar_doquote['add'] = '';
$ar_doquote['size'] = '';
$ar_doquote['rows'] = '';
$ar_doquote['headname'] = '"';
$ar_doquote['rawformat'] = '"';
$ar_doquote['image'] = '"';
$ar_doquote['allowhtml'] = '';
$ar_doquote['source'] = '"';
$ar_doquote['date_show_format'] = '"';
$ar_doquote['date_source_format'] = '"';
$ar_doquote['makefunction'] = '"';
$ar_doquote['list'] = '"';
$ar_doquote['listquery'] = '"';
$ar_doquote['listqueryorder'] = '"';
$ar_doquote['format_as'] = '"';
$ar_doquote['edit_as'] = '"';


// get query array


	$qry_get_fieldspec = "SELECT * FROM view_fieldspecs WHERE 1 ";
	
	$ar_data = get_array_query_full($qry_get_fieldspec, array(0=>array('var'=>'crudset_id', 'val'=>$crudset_id)));
	
	$text_out = "<?php \n";
	
	foreach($ar_data AS $rownum=>$row){
		foreach($row as $column=>$value){
		
		if($lastvar != $row['variable_name']){
			$text_out .= "\n //fieldspec for " . $row['variable_name'] . "\n";
		}
		
		if(($column != 'crudset_id') 
		AND ($column != 'variable_name')
		AND ($column != 'variable_id')
		AND ($column != 'fs_id')
		){
			if(!empty($value)){
			$text_out .= '$fieldspec["' . $row['variable_name'] . '"] ["' . $column . '"] = ' . $ar_doquote[$column] . $value . $ar_doquote[$column] . ';' . "\n";
			}
			$lastvar = $row['variable_name'];
		}

		}
		$text_out .= get_filters_array($crudset_id,$row);
		
	}
	$text_out .= "\n ?>";
	return $text_out;

}// end function

function get_filters_array($crudset_id,$row){

$variable_name = $row['variable_name'];
$variable_id = $row['variable_id'];




	$ar_data = get_array_query_full("SELECT * FROM filters WHERE 1 ", array(
	0=>array('var'=>'crudset_id', 'val'=>$crudset_id),
	1=>array('var'=>'col_id', 'val'=>$variable_id)
	));

	if(is_array($ar_data)){
	foreach($ar_data AS $filterline){
	$db .=  'HERE' . $filterarray . $crudset_id . ":" . $variable_id . "\n"; // DEBUG
	$filterparam = $filterline['filterparam'];
	
	if(empty($filterline['show'])){
		$show = 0;
	}else{
		$show = 1;
	}
	
	if((empty($filterline['source']) OR ($filterline['source'] == ' '))){
		$source = $row['source'];
	}	
	
$filterarray .= "\$fieldspec['$variable_name'] ['filter'] ['$filterparam']['op'] = '" . $filterline['op'] . "';
\$fieldspec['$variable_name'] ['filter'] ['$filterparam']['filterhead'] = '" . $filterline['filterhead'] . "';
\$fieldspec['$variable_name'] ['filter'] ['$filterparam']['flt_add'] = " . $filterline['flt_add'] . ";
\$fieldspec['$variable_name'] ['filter'] ['$filterparam']['flt_source'] = '" . $source . "';
\$fieldspec['$variable_name'] ['filter'] ['$filterparam']['show'] = " . $show . ";
";	


	}
	}	

	return $filterarray;
}
	


function write_to_file($crudset_id, $content){
	// get project_id
	$project_id = lookup_value("project_id","crudsets"," crudset_id = " . $crudset_id);
	
	$cs_name = lookup_value("cs_name","crudsets"," crudset_id = " . $crudset_id);

	$structure = lookup_value("file_structure","projects"," project_id = " . $project_id);
	
	$project_name = lookup_value("project_name","projects"," project_id = " . $project_id);


	// get structures path
	$structure_path = QC_SYS_BASEDIR . '/structures/';
	
	include $structure_path . $structure .  '/paths.php';	
	
	$outpath = $paths['fieldspec'];
	$outpath = str_replace('<setname>',$cs_name,$outpath);

	$outpath = QC_SYS_BASEDIR . '/' . QC_PROJECT_SUBPATH . $project_name . '/' . $outpath ;
	//echo "pname is " . $project_name;
	//echo $outpath;
	$outfile = $outpath . $cs_name . '_spec.php';

	
	makesuredir($outpath);
	
	
	file_put_contents($outfile,$content);
	

}

?>
